import { BUNDLE_TYPE_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import { AudioManager1 } from "../../manager/AudioManager1";
import { UIManager } from "../../manager/UIManager";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";
import Level_23_Base from "./Level_23_Base";
import LevelPre_23 from "./LevelPre_23";
const { ccclass, property } = cc._decorator;

let ANSWER: Array<Array<[string, boolean, string, string]>> = [];
let QUESION = [];

let WRONG_NUM = 3;

@ccclass
export default class Level_23_18 extends Level_23_Base {
    levelBase: LevelBase = null
    panduan: Boolean = true
    xuan: Boolean = false;
    jiesuan: Boolean = false;
    tianjiajieju: Boolean = false
    /** 更新选择 */
    gengxinxuanze = false;
    gN(nodeName) {
        return this.levelBase.getChildForName(nodeName)
    }

    start() {
        this.levelPre = cc.find("Canvas").getComponentInChildren(LevelPre_23);
        super.start();
        this.levelBase = cc.find("Canvas").getComponentInChildren(LevelBase);
        this.levelPre.node.on(cc.Node.EventType.TOUCH_START, this.npcTalkEnd, this)
        let isAutoAnswerActive = Tools.getStorage('setAutoAnswerActive')
        if (isAutoAnswerActive && isAutoAnswerActive != "false") {
            this.autoAnswer = true;
        } else {
            this.autoAnswer = false;
        }

        let isAnswerRightActive = Tools.getStorage('setAnswerRightActive')
        if (isAnswerRightActive && isAnswerRightActive != "false") {
            this.answerRight = true;
        } else {
            this.answerRight = false;
        }

        // this.levelPre.progress=7
    }
    initGame() {

        // this.scheduleOnce(() => {
        super.initGame();
        // }, 1)

    }
    getDelayTime(isRight: boolean) {
        if (this.levelPre.progress == 8) {
            return 5.5
        } else {
            return isRight ? 1 : 0.2;
        }
    }

    resetAnim() {
        this.mainSpine.node.active = true;
        // if (this.mainSpine.animation != "daiji") this.mainSpine.setAnimation(0, "pa", true);
        // if (this.npcSpine.animation != "daiji") this.npcSpine.setAnimation(0, "animation", true);
    }
    talkAnim() {
        // cc.log("talkAnim",this.levelPre.progress)
        // if(this.levelPre.progress == 9){
        //     this.gN("qiaomen").active = true;
        //     this.scheduleOnce(()=>{
        //         this.gN("qiaomen").active = false;
        //     },4)
        // }

    }
    npcTalkEnd() {
        if (this.gengxinxuanze) {
            return;
        }
        this.gengxinxuanze = true
        this.levelPre.content.active = true;
        this.updateOption();
        // super.npcTalkEnd();
        this.npcTalk.active = true;
        this.npcTalk.getComponent(LvItem).setLabString(QUESION[this.levelPre.progress][0]);
        cc.Tween.stopAllByTarget(this.npcTalk);
        this.npcSpine.setAnimation(0, "daiji", true);
        AudioManager1.stopAudio(this.NpcAudioId)
        UIManager.ins.enabledGameClick(true);
    }
    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_END, this.npcTalkEnd, this)
    }
    rightAnim() {
    }
    async wrongAnim() {
    }
    finish(finishStr: any): void {
        this.npcTalk.active = false;

        this.levelPre = cc.find("Canvas").getComponentInChildren(LevelPre_23);

        ANSWER = this.levelPre.interJson.answer;
        QUESION = this.levelPre.interJson.questions;
        WRONG_NUM = this.levelPre.interJson.wrongNum;

        // super.finish(finishStr);
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("ans")) {
            this.isSelect = true;
            let progress = Number(finishStr.split("_")[2]);
            let answer = Number(finishStr.split("_")[3]);
            this.levelPre.playQuestionAnim(false);
            this.checkAnswer(progress, answer - 1);
        }
        if (finishStr === "initGame") {
            this.initGame();

        }
        if (finishStr === "reviveGame") {
            if (this.xuan) {
                AudioManager1.playBG("恐怖背景音乐", 0.5, BUNDLE_TYPE_ENUM.LEVEL);
                this.mainSpine.setAnimation(0, "daiji", true)
                this.gN("符").active = false;
                this.gN("符 拷贝").active = false;
                this.gN("pengzhuang").active = false;
                this.gN("上划").active = false;
                this.gN("000").active = false;
                this.gN("符 拷贝").x = 330;
                this.gN("gzs").x = 385;
                this.gN("符 拷贝").y = 90;
                this.gN("上划").x = 328;
                this.gN("上划").y = 95;
                this.levelPre.progress = 3;
                this.wrongNum = 0;
                this.xuan = false;
                this.resetAnim();
                this.startGame();
            } else {
                AudioManager1.playBG("恐怖背景音乐", 0.5, BUNDLE_TYPE_ENUM.LEVEL);
                this.mainSpine.setAnimation(0, "daiji", true)
                this.npcSpine.setAnimation(0, "daiji", true)
                this.gN("符").active = false;
                this.gN("符 拷贝").active = false;
                this.gN("pengzhuang").active = false;
                this.gN("上划").active = false;
                this.gN("000").active = false;
                this.gN("符 拷贝").x = 330;
                this.gN("gzs").x = 385;
                this.gN("符 拷贝").y = 90;
                this.gN("上划").x = 328;
                this.gN("上划").y = 95;
                this.gN("nan").x = -330;
                this.gN("nan").y = -440;
                this.npcSpine.findBone("关门").active = false;
                this.gN("开门").active = true;
                this.levelPre.progress--;
                this.wrongNum = 0;
                this.xuan = false;
                this.resetAnim();
                this.startGame();
            }
        }
        if (finishStr === "jieguoSB") {
            if (this.tianjiajieju) {

            } else {
                this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("overGame").interTween);
            }
        }
        if (finishStr === "Panduan") {
            this.tianjiajieju = true;
            this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("winGame1").interTween);

        }
        if (finishStr === "shishiba") {
            if (this.xuan)//false
            {
                this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("winGame3").interTween);
            }
            else {
                this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("winGame2").interTween);
            }
        }
    }

    /** 检测答案对错 */
    async checkAnswer(progress: number, answer: number) {
        AudioManager1.stopAudio(this.NpcAudioId)
        let questions = ANSWER[progress];
        let txt = questions[answer][3] || questions[answer][0];
        let isRight = questions[answer][1];
        if (this.autoAnswer || this.answerRight) {
            isRight = true;
        }
        let audio = questions[answer][2];
        if (isRight === true && audio === "1") {
            AudioManager1.playAudio("xuandui", 1, BUNDLE_TYPE_ENUM.LEVEL);
        }
        let audioUrl = (progress + 1) + "" + audio;
        this.jiesuan = isRight;
        this.mainTalk.active = true;
        this.mainTalk.getComponent(LvItem).setLabString(txt);
        cc.Tween.stopAllByTarget(this.mainTalk);
        // this.mainSpine.setAnimation(0, "shuohua", true);

        this.MainAudioId = await AudioManager1.playAudio(audioUrl, 1, BUNDLE_TYPE_ENUM.LEVEL);
        let audioTime = cc.audioEngine.getDuration(this.MainAudioId);

        let time = this.getDelayTime(isRight);
        cc.tween(this.node).delay(audioTime).call(() => {
            if (!isRight) this.wrongNum += 1;
            this.levelPre.progress++;
            this.updateGameState();
            // cc.log("this.gameState == " + this.gameState, this.wrongNum)

            this.npcTalk.active = false;
            this.mainTalk.active = false
            if (isRight) {
                this.rightAnim();
                if (this.levelPre.progress === 4) {
                    if (audio === "2") {
                        this.xuan = true; // 第四个选错了的条件最后判断
                    } else if (audio === "1") {

                        this.xuan = false;
                        cc.tween(this.gN("nvren"))
                            .to(0.5, { position: cc.v3(900, 0) })
                            .start();
                    }
                }
                // else
                // {
                //     AudioManager.playAudio("xuandui", 1, BUNDLE_TYPE_ENUM.LEVEL);
                // }
                //判断第四个是否错  （audio）区分选项
                //错：条件 == false
                //对：条件 == true （用来最后判断用）
            } else {
                this.subHp();
                this.wrongAnim();
            }
        }).delay(time).call(() => {
            this.updateGame();
        }).start();
    }


    /** 开始一轮游戏 */
    async startGame() {
        this.isSelect = false;
        cc.log("当前第" + this.levelPre.progress + "题")
        this.gengxinxuanze = false
        UIManager.ins.enabledGameClick(true);
        this.npcTalk.active = true;
        this.npcTalk.getComponent(LvItem).setLabString(QUESION[this.levelPre.progress]);
        // this.npcSpine.setAnimation(0, "shuohua", true);
        this.talkAnim();
        this.NpcAudioId = await AudioManager1.playAudio("" + (1 + this.levelPre.progress), 1, BUNDLE_TYPE_ENUM.LEVEL);
        cc.Tween.stopAllByTarget(this.npcTalk);
        let audioTime = cc.audioEngine.getDuration(this.NpcAudioId);
        cc.tween(this.npcTalk).delay(audioTime).call(() => this.npcTalkEnd()).start();
    }
}